//
//  BottomCollectCell2.m
//  Zaker
//
//  Created by lirui on 16/3/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SeeBottomCollectCell2.h"
#import "SeeRadioCollectionCell.h"

#import "HeadView.h"
@interface SeeBottomCollectCell2 ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic ,retain)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *nameArr; //分区数组
@property (nonatomic, strong) NSMutableArray *tArr;  //radio字典转换成的数组
@property (nonatomic, strong) NSMutableArray *allArr; //radio字典转换成数组最后存放的大数组
@property(nonatomic, strong)NSMutableArray *cidArr; //cid的数组
@property(nonatomic, strong)UIButton * button;
@property(nonatomic, strong)NSMutableArray *tidArr; //tid 的数组
@property(nonatomic, strong)NSMutableArray *allTidArr; //所有tid 的数组
@property(nonatomic,assign)BOOL isrefresh;
@end
//static int i = 0;
@implementation SeeBottomCollectCell2


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {        
        [self data];
        [self creatCollection];
        [self.collectionView.mj_header beginRefreshing];
    }
    return self;
}

-(void)data
{ NSString *str = @"http://c.3g.163.com/nc/topicset/android/radio/index.html";
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
   
        self.allArr = [NSMutableArray array];
        self.allTidArr = [NSMutableArray array];
        self.nameArr = [NSMutableArray array];
        self.cidArr = [NSMutableArray array ];
       
        if (_isrefresh) {
            [_allArr removeAllObjects];
        }
              for (NSDictionary *dic in resposeObject[@"cList"]) {
            self.tArr = [NSMutableArray array];
            [_nameArr addObject:dic[@"cname"]];
                  [_cidArr addObject:dic[@"cid"]];
                  self.tidArr =[ NSMutableArray array];
            for (NSDictionary *tDic in dic[@"tList"]) {
               
                RadioModel *radioModel = [[RadioModel alloc] init];
                RadioModel *model = [[RadioModel alloc] init];
                [radioModel setValuesForKeysWithDictionary:tDic[@"radio"]];
                [model setValuesForKeysWithDictionary:tDic];
                [_tArr addObject:radioModel];
                [self.tidArr addObject:model.tid];
               
               
            }
                  /**所有radio的数组*/
            [_allArr addObject:_tArr];
                  /**所有tid的数组*/
                  [_allTidArr addObject:_tidArr];
                        }
        
        [_collectionView reloadData];
        [self.collectionView.mj_header endRefreshing];
          } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

#pragma mark -创建电台collectionview
-(void)creatCollection
{
    UICollectionViewFlowLayout *layout = [[ UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH*((375 - 40) / 3), 150 *HEIGHT);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing =1;

    self.collectionView = [[ UICollectionView alloc] initWithFrame:CGRectMake(10 * WIDTH, 0, WIDTH * 375 - 20*WIDTH, HEIGHT * 667) collectionViewLayout:layout];
    [_collectionView registerClass:[SeeRadioCollectionCell class] forCellWithReuseIdentifier:@"radiocell"];
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [ UIColor clearColor];
    _collectionView.contentInset = UIEdgeInsetsMake(0, 0, 120 *HEIGHT, 0);
    [self addSubview:_collectionView];
     self.button =[  UIButton buttonWithType:UIButtonTypeSystem];
        [self.button setTitle:@">" forState:UIControlStateNormal];
        self.button.tintColor = [UIColor grayColor];
        self.button .frame = CGRectMake(320 * WIDTH, HEIGHT*0, WIDTH*20, HEIGHT*50);
    [self.button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

    //头部区域注册
    [self.collectionView registerClass: [HeadView class]forSupplementaryViewOfKind:UICollectionElementKindSectionHeader  withReuseIdentifier:@"header"];
    
    layout.headerReferenceSize = CGSizeMake(100,HEIGHT * 50);
    
    
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingBlock:^{
        //赋初值
//        i = 1;
        self.isrefresh = YES;
        
        [self  data];
    }];
    self.collectionView.mj_header = header;
    
   

    
    
}
#pragma mark -头部区域方法
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    
     UICollectionViewLayoutAttributes *attri = [UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:UICollectionElementKindSectionHeader withIndexPath:indexPath];
    
    HeadView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
   
    attri.frame = CGRectMake(0, 0, self.frame.size.height, 50 * HEIGHT);
    [view applyLayoutAttributes:attri];
    [view.button addTarget: self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
    view.button.tag = indexPath.section;
    view.title.text = _nameArr[indexPath.section];
 
  

    return view;
}

#pragma mark -分区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    
    return _allArr.count;
}
#pragma mark -item个数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _tArr.count;
}
#pragma mark -cell设置
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SeeRadioCollectionCell *cell =[ collectionView dequeueReusableCellWithReuseIdentifier:@"radiocell" forIndexPath:indexPath];
    
    RadioModel  *model = _allArr[indexPath.section][indexPath.item];
    cell.tnameLabel.text = model.tname;
    cell.titleLabel.text = model.title;
    [cell.imgView sd_setImageWithURL:[NSURL URLWithString:model.imgsrc]];
    
         return cell;
}
-(void)click:(UIButton *)btn
{
    
    self.btnClick(self.cidArr[btn.tag]);
   

}
    
    

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    self.collectionblock(_allTidArr[indexPath.section][indexPath.item] );
    
}

@end
